package com.example.myapp

import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import com.example.myapp.navigation.navigateToAbout
import com.example.myapp.navigation.goBack

@Composable
fun HomeScreen(
    navController: NavController,
    useSafeArea: Boolean = true // 添加控制安全距离的参数
) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(if (useSafeArea) 16.dp else 0.dp), // 根据useSafeArea决定padding
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.spacedBy(16.dp)
    ) {
        Text(
            text = "首页",
            fontSize = 24.sp,
            fontWeight = FontWeight.Bold
        )
        
        Spacer(modifier = Modifier.height(32.dp))
        
        Text(
            text = "欢迎来到首页！这是一个使用Jetpack Compose构建的Android应用。",
            fontSize = 16.sp,
            modifier = Modifier.padding(16.dp)
        )
        
        // 显示全局状态
        Card(
            modifier = Modifier.fillMaxWidth(),
            elevation = CardDefaults.cardElevation(defaultElevation = 4.dp)
        ) {
            Column(
                modifier = Modifier.padding(16.dp)
            ) {
                Text(
                    text = "全局状态信息",
                    fontSize = 18.sp,
                    fontWeight = FontWeight.Bold
                )
                
                Spacer(modifier = Modifier.height(8.dp))
                
                Text("计数器: ${GlobalState.counter.value}")
                Text("共享文本: ${GlobalState.sharedText.value}")
                
                Spacer(modifier = Modifier.height(16.dp))
                
                Row(
                    horizontalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    Button(
                        onClick = { GlobalState.incrementCounter() }
                    ) {
                        Text("增加计数器")
                    }
                    
                    Button(
                        onClick = { GlobalState.updateSharedText("从首页更新的文本 - ${System.currentTimeMillis()}") }
                    ) {
                        Text("更新文本")
                    }
                }
            }
        }
        
        Spacer(modifier = Modifier.weight(1f))
        
        Row(
            horizontalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            Button(
                onClick = { navController.navigateToAbout() }
            ) {
                Text("跳转到关于页面")
            }
            
            Button(
                onClick = { navController.goBack() }
            ) {
                Text("返回")
            }
        }
    }
} 